<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

/**
 * 曝光流水
 * 
 **/

class Exposure implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * Create a new job instance.
     *
     * $exposure['type'] = 'feed';
     * $exposure['uuids'] = $feed_uuids; //['a-1','b-2']
     * @return void
     */
    public function __construct($data)
    {
        //
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $insert_res = [];
            foreach ($this->data['uuids'] as $key => $value) {
                
                $insert_res[] = ['service_uuid' => $value, 'service' => $this->data['type']];
            }

            if(!empty($insert_res)){
                DB::table('com_exposure')->insert(
                    $insert_res
                );
            }
        } catch (\Exception $exception) {
             $this->fail($exception);
        }
    }

    /**
     * 
     **/
    public function fail($exception)
    {
        //
        Log::alert($exception->getMessage());   
    }
}


